<?php
/**
 * Created by PhpStorm.
 * User: kamlyli
 * Date: 2017/8/5
 * Time: 19:30
 */

namespace app\api\controller\v1;


use app\api\model\ProblemDetail;
use think\Request;

class Practice extends Base
{
    public function get_question($type, Request $request)
    {
        // 检查是不是这个人
        $this->check_cookie();

        // 校验数据 type

        // 随机题目
        $problem_detail = new ProblemDetail();
        $problem_detail = $problem_detail->where('type', $type)->field(['id', 'type', 'des', 'img', 'optionA', 'optionB', 'optionC', 'optionD'])->select()->toArray();
        $random_num = array_rand($problem_detail);

        // 返回内容
        return json_info($problem_detail[$random_num]);
    }

    public function answer_question(Request $request)
    {
        // 检查是不是这个人
        $this->check_cookie();

        // 校验数据 type , id , answer,

        // 获取  type , id , answer,
        $param = $request->param();

        // 判断是不是正确
        $problem_detail = new ProblemDetail();
        $problem_detail = $problem_detail->where('id', $param['id'])->find()->toArray();
        if ($problem_detail['optionRight'] == $param['answer']) {
            $data = [
                'right' => 1,
                'message' => '正确'
            ]; // 正确
        } else {
            $data = [
                'right' => 0,
                'message' => '错误'
            ]; // 错误
        }
        return json_info($data);
    }
}